package com.yaowk.platform.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.activerecord.tx.TxConfig;
import com.yaowk.common.kit.Constant;
import com.yaowk.common.kit.I18nKit;
import com.yaowk.common.kit.ResultKit;
import com.yaowk.common.validator.RequiredIdValidator;
import com.yaowk.platform.model.AuditConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.List;

/**
 * 审核配置
 *
 * @authc yaowk
 * 2017/4/30
 */
@TxConfig(Constant.PLATFORM_MODULE)
public class AuditConfigController extends Controller {

    /**
     * 审核配置列表
     *
     * @param id|平台id|Integer|必填
     * @title 审核配置列表
     */
    @Before({ RequiredIdValidator.class, Tx.class })
    @RequiresPermissions("platform:read")
    public void list() {
        Integer platformId = getParaToInt("id");
        List<AuditConfig> auditConfigList = AuditConfig.dao.findByPlatformId(platformId);
        boolean trade = true;
        boolean qualification = true;
        boolean name = true;
        // 判断是否有行业和资质
        for (AuditConfig auditConfig : auditConfigList) {
            if (auditConfig.getType() == 2) {
                trade = false;
            }
            if (auditConfig.getType() == 3) {
                qualification = false;
            }
            if (auditConfig.getType() == 4){
                name = false;
            }
        }

        // 没有行业
        if (trade) {
            AuditConfig auditConfig = new AuditConfig();
            auditConfig.setName(I18nKit.get("trade", Constant.PLATFORM_MODULE, this))
                    .setMessage(I18nKit.get("trade_message", Constant.PLATFORM_MODULE, this))
                    .setRequire(1)
                    .setAccessory(0)
                    .setType(2)
                    .setStatus(1)
                    .setSort(0)
                    .setPlatformId(platformId)
                    .save();
            auditConfigList.add(auditConfig);
        }

        // 没有资质
        if (qualification) {
            AuditConfig auditConfig = new AuditConfig();
            auditConfig.setName(I18nKit.get("qualification", Constant.PLATFORM_MODULE, this))
                    .setMessage(I18nKit.get("qualification_message", Constant.PLATFORM_MODULE, this))
                    .setRequire(0)
                    .setAccessory(1)
                    .setType(3)
                    .setStatus(1)
                    .setSort(0)
                    .setPlatformId(platformId)
                    .save();
            auditConfigList.add(auditConfig);
        }

        // 没有名称
        if (name){
            AuditConfig auditConfig = new AuditConfig();
            auditConfig.setName(I18nKit.get("name", Constant.PLATFORM_MODULE, this))
                    .setMessage(I18nKit.get("name_message", Constant.PLATFORM_MODULE, this))
                    .setRequire(0)
                    .setAccessory(1)
                    .setType(4)
                    .setStatus(1)
                    .setSort(0)
                    .setPlatformId(platformId)
                    .save();
            auditConfigList.add(auditConfig);
        }
        renderJson(ResultKit.success(auditConfigList));
    }

    /**
     * 添加审核配置
     *
     * @param platform_id|平台id|Integer|必填
     * @param name|名称|String|否
     * @param message|提示语|String|否
     * @param require|必填1-是0-否|Integer|否
     * @param accessory|附件证明1-是0-否|Integer|否
     * @title 添加审核配置
     */
    @RequiresPermissions("platform:update")
    public void add() {
        AuditConfig auditConfig = getModel(AuditConfig.class, "");
        auditConfig.save();
        renderJson(ResultKit.success(this));
    }

    /**
     * 修改审核配置
     *
     * @param id|id|Integer|必填
     * @param name|名称|String|否
     * @param message|提示语|String|否
     * @param require|必填1-是0-否|Integer|否
     * @param accessory|附件证明1-是0-否|Integer|否
     * @param sort|排序|Integer|否
     * @title 修改审核配置
     */
    @Before(RequiredIdValidator.class)
    @RequiresPermissions("platform:update")
    public void edit() {
        AuditConfig auditConfig = getModel(AuditConfig.class, "");
        AuditConfig old = AuditConfig.dao.findById(auditConfig.getId());
        old.setName(auditConfig.getName())
                .setMessage(auditConfig.getMessage())
                .setRequire(auditConfig.getRequire())
                .setAccessory(auditConfig.getAccessory())
                .setSort(auditConfig.getSort())
                .update();
        renderJson(ResultKit.success(this));
    }

    /**
     * 删除配置
     *
     * @param id|id|Integer|必填
     * @title 删除配置
     */
    @Before(RequiredIdValidator.class)
    @RequiresPermissions("platform:update")
    public void remove() {
        Integer id = getParaToInt("id");
        AuditConfig.dao.findById(id).setStatus(0).update();
        renderJson(ResultKit.success(this));
    }
}
